package com.zx.map.service.impl;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zx.map.dao.mapper.EmployeeMapper;
import com.zx.map.dao.model.Employee;
import com.zx.map.dao.model.EmployeeExample;
import com.zx.map.service.EmployeeService;
import com.zx.platform.utils.PmpUtils;

@Component("employeeServiceImpl")
public class EmployeeServiceImpl implements EmployeeService {
	@Autowired
	private EmployeeMapper mapper;

	@Override
	public int add(Employee e) {
		mapper.insert(e);
		return 0;
	}

	@Override
	public int delete(Integer id) {
		mapper.deleteByPrimaryKey(id);
		return 0;
	}

	@Override
	public List<Employee> list(String shopId) {
		EmployeeExample example = new EmployeeExample();
		EmployeeExample.Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(shopId)) {
			criteria.andMerchantidEqualTo(shopId);
		}
		example.setOrderByClause("id desc");

		return mapper.selectByExample(example);
	}

	@Override
	public Employee updatePassword(Employee e) {
		try {
			Employee es= mapper.selectByPrimaryKey(e.getId());
			es.setPassword(PmpUtils.encoderByMD5(e.getPassword()));
			mapper.updateByPrimaryKey(es);
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		
		return e;
	}

	@Override
	public Employee get(Integer id) {
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public Employee getEmployeeByUserId(String userId) {
		EmployeeExample example = new EmployeeExample();
		EmployeeExample.Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(userId)) {
			criteria.andMobileEqualTo(userId);
		}
		List<Employee> l = mapper.selectByExample(example);
		if (l != null && l.size() > 0) {
			return l.get(0);
		}
		return null;
	}

}
